package com.example.backspring.Service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.backspring.Service.GoalsService;
import com.example.backspring.Service.LearnService;
import com.example.backspring.config.Message;
import com.example.backspring.config.SnowflakeSixDigits;
import com.example.backspring.dao.GoalMapper;
import com.example.backspring.entity.Goal;
import com.example.backspring.entity.Learn;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GoalsServiceImpl implements GoalsService {
    @Resource
    private GoalMapper goalMapper;
    @Resource
    private LearnServiceImpl learnService;

    @Override
    public Message addGoal(String goal) {
        Message result = new Message();
        String xueTime = learnService.getDate(0, "f");
        int status = 0; // 使用更有意义的变量名代替 pod
        SnowflakeSixDigits snowflake = new SnowflakeSixDigits();
        long idwoq = snowflake.nextId();
        long l = idwoq % 100000000; // 取后六位数字
        int id = Integer.parseInt(String.valueOf(l));
        Integer res = goalMapper.addGoal(id, xueTime, goal, status, "", "", "", "", "");
        if (res == 1) {
            result.setSuccess(true);
            result.setMessage("添加目标成功");
            result.setStatus(Message.SUCCESS_STATUS); // 假设Message类定义了SUCCESS_STATUS常量
        } else {
            result.setSuccess(false);
            result.setMessage("添加失败");
            result.setStatus(Message.FAIL_STATUS); // 假设Message类定义了FAIL_STATUS常量
        }
        return result;
    }

    @Override
    public Message getGoals() {
        Message result = new Message();
        JSONArray data = new JSONArray();
        List<Goal> res = goalMapper.getGoals();
        if (res.size() > 0) {
            for (Goal learn : res) {
                JSONObject map = new JSONObject();
                map.put("id", learn.getId());
                map.put("sj", learn.getSj().toString());
                map.put("nr", learn.getNr().toString());
                map.put("a", learn.getJtbz().toString());
                map.put("b", learn.getKhl().toString());
                map.put("c", learn.getKsx().toString());
                map.put("d", learn.getXgx().toString());
                map.put("e", learn.getYsx().toString());
                data.add(map);
            }
            result.setMessage(data);
            result.setSuccess(true);
            result.setStatus(1);
        } else {
            result.setSuccess(false);
            result.setMessage("暂无目标数据");
            result.setStatus(0);
        }
        return result;
    }

    @Override
    public Message deleteGoal(int id) {
        return null;
    }

    @Override
    public Message updateGoal(Integer id, String lie, String nr) {
        Message result = new Message();
        log.info("id:{},lie:{},nr:{}", id, lie, nr);
        int res = 0;
        if ("a".equals(lie)) {
            res = goalMapper.updateA(id, nr);
        } else if ("b".equals(lie)) {
            res = goalMapper.updateB(id, nr);
        } else if ("c".equals(lie)) {
            res = goalMapper.updateC(id, nr);
        } else if ("d".equals(lie)) {
            res = goalMapper.updateD(id, nr);
        } else if ("e".equals(lie)) {
            res = goalMapper.updateE(id, nr);
        }else if ("nr".equals(lie)) {
            res = goalMapper.updateNR(id, nr);
        }
        if (res == 1) {
            result.setSuccess(true);
            result.setMessage("更新成功");
            result.setStatus(Message.SUCCESS_STATUS); // 假设Message类定义了SUCCESS_STATUS常量
        } else {
            result.setSuccess(false);
            result.setMessage("更新数据库失败");
            result.setStatus(Message.FAIL_STATUS); // 假设Message类定义了FAIL_STATUS常量
        }
        return result;
    }
}
